//
//  LBCropPanelView.m
//  LBCropImagePage
//
//  Created by mac on 2024/6/2.
//

#import "LBCropPanelView.h"
#import "LBMediaRotateScroller.h"

@interface LBCropPanelView () <LBMediaRotateScrollerDelegate>

@property (nonatomic, strong) LBMediaRotateScroller *scroller;

@property (nonatomic, strong) UIView *toolbar;

@property (nonatomic, strong) UIButton *rotateBtn;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIButton *doneBtn;
@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) NSArray <UIButton *> *btns;

@property (nonatomic, assign) kCropOriginAngle rotate;

@property (nonatomic, assign) kCropRatioType ratio;


@end

@implementation LBCropPanelView

- (instancetype)initWithFrame:(CGRect)frame
                        ratio:(kCropRatioType)ratio
                        angle:(CGFloat)angle
                       rotate:(kCropOriginAngle)rotate
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor darkGrayColor];
        [self customLayout];
        [self setupToolbar];
        self.angle = angle;
        self.rotate = rotate;
        self.ratio = ratio;
        if (ratio < self.btns.count) {
            UIButton *btn = self.btns[ratio];
            btn.selected = YES;
        }
    }
    return self;
}

- (void)customLayout
{
    [self addSubview:self.scroller];
    [self addSubview:self.toolbar];
    [self addSubview:self.doneBtn];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.titleLabel];
    [self addSubview:self.rotateBtn];
    self.toolbar.frame = CGRectMake(0, CGRectGetHeight(self.scroller.bounds) + adapt375(12), CGRectGetWidth(self.bounds), adapt375(46));
    self.cancelBtn.frame = CGRectMake(16, CGRectGetHeight(self.bounds) - kBottomSafeHeight - 44, 44, 44);
    self.doneBtn.frame = CGRectMake(CGRectGetWidth(self.bounds) - 16 - 44, CGRectGetHeight(self.bounds) - kBottomSafeHeight - 44, 44, 44);
    [self.titleLabel sizeToFit];
    CGPoint center = self.titleLabel.center;
    center.x = CGRectGetWidth(self.bounds)/2;
    center.y = CGRectGetMidY(self.cancelBtn.frame);
    self.titleLabel.center = center;
}

- (void)setAngle:(CGFloat)angle
{
    _angle = angle;
    [self.scroller updateWithAngle:angle];
}

- (kCropRatioType)cropRatioType
{
    return self.ratio;
}

- (void)setupToolbar
{
    CGFloat itemSpace = adapt375(32);
    CGSize itemSize = CGSizeMake(adapt375(28), adapt375(45));
    CGFloat leftMargin = (CGRectGetWidth(self.bounds) - itemSize.width * 5 - itemSpace * 4) / 2;
    NSMutableArray *btns = [NSMutableArray array];
    for (NSInteger i = 0; i < 5; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.highlighted = NO;
        btn.tag = i; //对应的枚举kCropRatioType
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        switch ((kCropRatioType)btn.tag) {
            case kCropRatioTypeOri:
                [btn setTitle:@"原始" forState:UIControlStateNormal];
                break;
            case kCropRatioType11:
                [btn setTitle:@"1:1" forState:UIControlStateNormal];
                break;
            case kCropRatioType34:
                [btn setTitle:@"3:4" forState:UIControlStateNormal];
                break;
            case kCropRatioType43:
                [btn setTitle:@"4:3" forState:UIControlStateNormal];
                break;
            case kCropRatioType916:
                [btn setTitle:@"9:16" forState:UIControlStateNormal];
                break;
            default:
                break;
        }
        btn.frame = CGRectMake(leftMargin + i * (itemSize.width + itemSpace), 0, itemSize.width, itemSize.height);
        [btn addTarget:self action:@selector(cropBtnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        [btns addObject:btn];
        [self.toolbar addSubview:btn];
    }
    self.btns = btns;
}

- (void)doneClick
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(panelDidCompleteWithPanelType:)]) {
        [self.delegate panelDidCompleteWithPanelType:[self getCurrentPanelType]];
    }
}

- (void)cancelClick
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(panelDidCancelWithPanelType:)]) {
        [self.delegate panelDidCancelWithPanelType:[self getCurrentPanelType]];
    }
}

- (void)didClickRotate
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(rotateRight)]) {
        [self.delegate rotateRight];
    }
}

- (void)cropBtnDidClicked:(UIButton *)btn
{
    self.ratio = (kCropRatioType)btn.tag;
    if (!btn.isSelected) {
        return;
    }
    
    for (UIButton *btn in self.btns) {
        btn.selected = NO;
    }
    btn.selected = YES;
    if (self.delegate && [self.delegate respondsToSelector:@selector(cropWithRatioType:)]) {
        [self.delegate cropWithRatioType:(kCropRatioType)btn.tag];
    }
}

- (void)showPanelWithSuperView:(UIView *)view 
                       sibling:(dispatch_block_t)sibling
{
    [self showPanelWihtSuperView:view sibling:sibling completion:nil];
}

- (void)showPanelWihtSuperView:(UIView *)view 
                       sibling:(dispatch_block_t)sibling
                    completion:(dispatch_block_t)completion
{
    if (!view) {
        return;
    }
    [view addSubview:self];
    ;
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alpha = 1;
        CGRect frame = self.frame;
        CGPoint origin = self.frame.origin;
        origin.y = view.bounds.size.height - self.bounds.size.height;
        frame.origin = origin;
        self.frame = frame;
        if (sibling) {
            sibling();
        }
    } completion:^(BOOL finished) {
        if (completion) {
            completion();
        }
    }];
}

- (void)layoutSubviews
{
    CGRect frame = CGRectMake(CGRectGetWidth(self.bounds) - 6 - 40, CGRectGetHeight(self.scroller.frame) - 18 - 20, 40, 40);
    self.rotateBtn.frame = frame;
}

- (void)rotateScroller:(LBMediaRotateScroller *)scroller percent:(CGFloat)percent
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cropPanel:roate:)]) {
        [self.delegate cropPanel:self roate:percent];
    }
}

- (NSString *)getCurrentPanelType
{
    return @"crop";
}

#pragma mark - lazy load

- (LBMediaRotateScroller *)scroller
{
    if (!_scroller) {
        _scroller = [[LBMediaRotateScroller alloc] initWithFrame:CGRectMake(60, 16, CGRectGetWidth(self.bounds) - 120, 56)];
        _scroller.backgroundColor = [UIColor redColor];
        _scroller.delegate = self;
    }
    return _scroller;
}

- (UIButton *)rotateBtn
{
    if (!_rotateBtn) {
        _rotateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rotateBtn setTitle:@"旋转" forState:UIControlStateNormal];
        _rotateBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_rotateBtn addTarget:self action:@selector(didClickRotate) forControlEvents:UIControlEventTouchUpInside];
        [_rotateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _rotateBtn;
}

- (UIView *)toolbar
{
    if (!_toolbar) {
        _toolbar = [[UIView alloc] init];
    }
    return _toolbar;
}

- (UIButton *)doneBtn
{
    if (!_doneBtn) {
        _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneBtn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
        _doneBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_doneBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _doneBtn;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _cancelBtn;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.text = @"裁剪";
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
